home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 1800 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.4 KB

  1. Path: news.umbc.edu!not-for-mail
  2. From: schlein@umbc.edu (Jonas J. Schlein)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Recursion in C pre-processor ?
  5. Date: 16 Jan 1996 16:34:45 -0500
  6. Organization: University of Maryland Baltimore County
  7. Message-ID: <4dh5ll$2i4@umbc9.umbc.edu>
  8. References: <30FC134D.3A8F@powertech.no>
  9. NNTP-Posting-Host: f-umbc9.umbc.edu
  10. NNTP-Posting-User: schlein
  11.  
  12. Geir Thomassen  <geirt@powertech.no> wrote:
  13. |> I have a function declared as
  14. |> 
  15. |>         void func(void);
  16. |> 
  17. |> In a typical application, the call to func() is repeated many times in 
  18. |> a row:
  19. |> 
  20. |>         func(); func(); func(); ..... ; func();
  21. |> 
  22. |> To make my code more readable I would like a macro DoFunc(n) which expands
  23. |> to n calls to func(). I tried to make a recursive macro, but it did not 
  24. |> pass cpp.
  25. |> 
  26. |> #define DoFunc(n)\              /* Don't work !! */
  27. |> #if n\
  28. |>    func();\
  29. |>    DoFunc(n-1);\
  30. |> #endif
  31. |> 
  32. |> Why ?? Does cpp stop expanding the #if n ... #endif in the first pass 
  33. |> if n=0, or does it expand forever ??
  34.  
  35. Because macros are done by the preprocessor and not at runtime. Since
  36. 'n' is a variable it can't be done this way.
  37.  
  38. |> Does anyone have a better (working) idea ?? The idea of recursion is 
  39. |> probably a wrong track! Please help !!
  40.  
  41. Not necessarily wrong, but perhaps needless in a problem which is
  42. clearly repeating the *SAME* task 'n' times rather than smaller
  43. sub-problems of the same form as the original problem.
  44.  
  45. |> Why do I want to do this ?? The function func() emits a NOP assembly
  46. |> instruction into the instruction stream. The code is for a slow 8051 
  47. |> micro controller  with 1 us instruction cycle, and the NOPs are used 
  48. |> for timing. DoFunc(5) would give me 5 us delay, in just 5 bytes of code !!
  49.  
  50. I'd recommend to just do it with a for() look inside of another function
  51. called DoFunc(). If an additional function call is too much overhead,
  52. you're already calling func() 'n' times so what's one more function ;-),
  53. then a MACRO is possible. Here's a solution to that effect if you really want
  54. to do it that way:
  55.  
  56. #define DoFunc(n) do { int i; for (i = 0; i < n; i++) func (); } while (0)
  57.  
  58. The do-while wrapper is a popular idiom to allow you to call your macro
  59. as if it were a function, i.e. end it with a semi-colon. It also gives
  60. you another block where variables can be temporarily declared and used.
  61. -- 
  62. "If it wasn't for C, we would be using BASI, PASAL, and OBOL."
  63.  
  64. Jonas J. Schlein  (schlein@gl.umbc.edu)
  65.